package com.hualongdata.util.poi;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import scala.collection.JavaConversions;
import scala.collection.JavaConverters;
import scala.collection.mutable.Buffer;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by hldev on 16-10-25.
 */
public class ExcelImport {

    // 把一个字符串的第一个字母大写、效率是最高的、
    private static String getMethodName(String fildeName) {
        byte[] items = fildeName.getBytes();
        items[0] = (byte) ((char) items[0] - 'a' + 'A');
        return new String(items);
    }

    public static void main(String[] strs) {
        //  new ExcelImport().ExcelImport(User.class,new String[]{"age","name"});


        try {
            XSSFWorkbook workbook = (XSSFWorkbook) WorkbookFactory.create(new File("/home/hldev/Documents/信息填写.xlsx"));
            XSSFSheet sheet = workbook.getSheet("地方编码");
            Iterator i = sheet.iterator();

            while (i.hasNext()) {
                Row row = (Row) i.next();
                System.out.println(row.getCell(1));
//               Iterator ii =  row.iterator();
//                Dfbm dfbm = new Dfbm();
//                while(ii.hasNext()){
//                    dfbm.setDfbm((String)ii.next());
//                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List createBean(Class clazz, String[] fileds, String fileName, int step_lines) {
        step_lines = 0;//默认不跳过
        List<Struct> methods = new ArrayList<>();
        List<Object> objects = new ArrayList<>();
        try {
            Object obj = Class.forName(clazz.getName()).newInstance();
//            Field[] fields = clazz.getDeclaredFields();
            for (int i = 0; i < fileds.length; i++) {
                System.out.println("infoTypeName:" + fileds[i]);
                Method get_Method = clazz.getMethod("get" + getMethodName(fileds[i]));  //获取
                Method set_Method = clazz.getMethod("set" + getMethodName(fileds[i]), get_Method.getReturnType());
                Class<?> c = get_Method.getReturnType();
                methods.add(i, new Struct(get_Method, set_Method, c));
//                Class<?> c = get_Method.getReturnType();
//                String type = c.getName();
//                System.out.println("类型："+type);
//                Object o = clazz.newInstance();
//                set_Method.invoke(obj, 2);
            }
        } catch (Exception e) {

            e.printStackTrace();
        }


        try {
            XSSFWorkbook workbook = (XSSFWorkbook) WorkbookFactory.create(new File("/home/hldev/Documents/信息填写.xlsx"));
            XSSFSheet sheet = workbook.getSheet(fileName);
            Iterator i = sheet.iterator();
            int tmp = 0;

            while (i.hasNext()) {
                if (tmp > step_lines - 1) { //排查表头
                    Object o = clazz.newInstance();
                    Row row = (Row) i.next();
                    // System.out.println(row.getCell(1));
                    int size = methods.size();
                    for (int j = 0; j < size; j++) {
                        Struct s = methods.get(j);
                        String type = s.getType().toString();
                        Cell cell = row.getCell(j);
                        if (type.equals("class java.lang.String"))
                            s.getSet_Method().invoke(o, getStringValue(cell));
                        if (type.equals("class java.lang.Integer"))
                            s.getSet_Method().invoke(o, Integer.parseInt(getStringValue(cell)));
                        if (type.equals("class java.lang.Long"))
                            s.getSet_Method().invoke(o, Long.parseLong(getStringValue(cell)));
                        if (type.equals("class java.lang.Boolean"))
                            s.getSet_Method().invoke(o, getStringValue(cell));
                    }
                    objects.add(o);

                } else {
                    Row row = (Row) i.next();
                    tmp++;
                }

            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        //  new WorkbookFactory();
        return objects;
    }

    public String getStringValue(Cell cell) {
        String value = "";
        if (cell.getCellTypeEnum() == CellType.STRING)
            value = cell.getStringCellValue();
        if (cell.getCellTypeEnum() == CellType.NUMERIC) {
            value = cell.getNumericCellValue() + "";
            if (value.contains(".")) {
                value = value.split("\\.")[0];
            }
        }
        // TODO 其实类型的值未获取
        return value;
    }

    class Struct {
        Method get_Method;
        Method set_Method;
        Class<?> type;

        public Struct(Method get_Method, Method set_Method, Class<?> type) {
            this.get_Method = get_Method;
            this.set_Method = set_Method;
            this.type = type;
        }

        public Method getGet_Method() {
            return get_Method;
        }

        public Method getSet_Method() {
            return set_Method;
        }

        public Class<?> getType() {
            return type;
        }
    }

}
